package com.iclub.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.iclub.model.IClubUser;
import com.iclub.util.Constants;
import com.iclub.util.MD5Util;

/**
 * select * from t_user_merchant WHERE merchantId=1 UNION select * from
 * t_user_merchant WHERE userId='1' order by userId Desc
 * 
 * select u.* from t_IClubUser u,t_friendlist f where f.userId='1' and
 * u.userId=f.friendId
 * 
 * @author hp
 *
 */
@Controller
@RequestMapping("/user")
public class IClubUserController extends BaseController {

	@RequestMapping("/delFriend.do")
	public void delFriend(HttpServletRequest request, HttpServletResponse response, IClubUser user)
			throws IOException {
		Map<String, String> req = new HashMap<String, String>();
		log_detail.info("删除好友" + req);
		Map<String, Object> res = iClubUserServiceImpl.addFriend(req);
		log_detail.info("响应结果" + res);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	@RequestMapping("/addFriend.do")
	public void addFriend(HttpServletRequest request, HttpServletResponse response, IClubUser user)
			throws IOException {
		Map<String, String> req = new HashMap<String, String>();
		log_detail.info("添加好友" + req);
		Map<String, Object> res = iClubUserServiceImpl.addFriend(req);
		log_detail.info("响应结果" + res);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	@RequestMapping("/getFriendList.do")
	public void getFriendList(HttpServletRequest request, HttpServletResponse response,
			IClubUser user) throws IOException {
		Map<String, String> req = new HashMap<String, String>();
		log_detail.info("获取好友列表" + req);
		Map<String, Object> res = iClubUserServiceImpl.selectIClubUserList(req);
		log_detail.info("响应" + res);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	//获取用户信息
	@RequestMapping("/getUserInfo.do")
	public void getUserInfo(HttpServletRequest request, HttpServletResponse response, String userId)
			throws IOException {
		Map<String, String> req = new HashMap<String, String>();
		req.put("userId", userId);
		Map<String, Object> res = iClubUserServiceImpl.selectIClubUser(req);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}
	//用户登录鉴权
	@RequestMapping("/checkUser.do")
	public void checkUser(HttpServletRequest request, HttpServletResponse response, IClubUser user)
			throws IOException {
		// 组装参数
		Map<String, String> req = new HashMap<String, String>();
		req.put("username", user.getUsername());
		req.put("password", user.getPassword());
		log_detail.info("[用户管理模块] 用户登录鉴权  请求数据为" + req);
		Map<String, Object> res = iClubUserServiceImpl.checkIClubUser(req);
		log_detail.info("[用户管理模块] 用户登录鉴权  返回数据为" + user);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);

	}

	@RequestMapping("/updateUserInfo.do")
	public void updateUserInfo(HttpServletRequest request, HttpServletResponse response,
			IClubUser user) throws IOException {
		Map<String, Object> res = iClubUserServiceImpl.updateUser(user);
		String json = JSON.toJSONString(res);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);

	}

	@RequestMapping("/registUser.do")
	public void registUser(HttpServletRequest request, HttpServletResponse response, IClubUser user)
			throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", user.getUsername());
		Map<String, String> res = iClubUserServiceImpl.checkRegist(map);
		log_detail.info("[用户管理模块] 用户注册  验证用户是否已经注册");
		if (res.get(Constants.KEY_RESMSG).equals(Constants.ALREADYREGIST_RESMSG)) {
			String json = JSON.toJSONString(res);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
			log_detail.info("[用户管理模块] 用户注册  验证用户已经注册"+ json);
			return;
		}
		// 主键为UUID
		user.setUserId(getPrimaryKey());
		// 密码进行MD5加密
		user.setPassword(MD5Util.MD5(user.getPassword() + encryptkey));
		log_detail.info("[用户管理模块] 用户注册  开始向后台请求用户注册:username=" + user.getUsername());
		Map<String, Object> ret = iClubUserServiceImpl.insertIClubUser(user);
		String json = JSON.toJSONString(ret);
		log_detail.info("[用户管理模块] 用户注册  返回数据为" + json);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json);
	}

	private String encryptkey;

	@Value("${encryptkey}")
	public void setEncryptkey(String encryptkey) {
		this.encryptkey = encryptkey;
	}

}
